<template>
  <div class="flex-box" ref="tableContainerRef" style="position: relative;">
    <!-- 其他条件 -->
    <div class="width100" ref="tableOtherRef"> 
      <slot name="other"></slot>
    </div>
    <!-- 查询条件 -->
    <div class="search-box" ref="tableHeaderRef" v-show="props.showForm">
      <div class="operation" v-show="props.showPanel">
        <el-tooltip content="刷新当前页面" placement="top">
          <el-icon class="icon-item" size="20" @click="refreshPage" v-show="props.showSet"><Refresh/></el-icon>
        </el-tooltip>
        <el-tooltip content="设置当前页面表格选项" placement="top">
          <el-icon class="icon-item marginL10" size="20" @click="changeTh" v-show="props.showSet"><Setting/></el-icon>
        </el-tooltip>
      </div>
      <el-form :inline="true" class="demo-form-inline">
        <slot name="form"></slot>
        <el-form-item v-show="props.showFromQuery">
          <el-button icon="Search" type="primary" :disabled="indexStore.loading" @click="getSearchList">查询</el-button>
          <el-button icon="Refresh" type="info" size="small" @click="getResetList">重 置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 数据表格 -->
    <div class="table-box">
      <div class="flexBetween paddingB10 width100" ref="tableButtonRef">
        <el-row ref="button" class="search-button">
          <el-col :span="24">
            <slot name="button"></slot>
          </el-col>
        </el-row>
        <div class="icon-box marginR20" v-show="!props.showPanel">
          <el-tooltip content="设置当前页面表格选项" placement="top">
            <el-icon class="icon-item" size="20" @click="changeTh" v-show="props.showSet"><Setting/></el-icon>
          </el-tooltip>
          <el-tooltip content="导出" placement="top">
            <el-icon class="icon-item" size="20" @click="exportDownload" v-show="props.showExport"><Download/></el-icon>
          </el-tooltip>
          <el-tooltip content="刷新当前页面" placement="top">
            <el-icon class="icon-item" size="20" @click="refreshPage" v-show="props.showSet"><Refresh/></el-icon>
          </el-tooltip>
        </div>
      </div>
      <el-table :data="list" v-loading="indexStore.loading" @sort-change="sortTable" :key='key' :row-class-name="tableRowClassName"
        style="width: 100%;" :style="{height:data.tableContentHeight}" @select-all="selectTable" @select="selectTable">
        <template #empty v-show="list && !list.length">
          <div class="noDataAvailable">
            <img src="@/assets/images/nodata.png" alt="" />
            <p>~ 暂无数据 ~</p>
          </div>
        </template>
        <slot name="table"></slot>
      </el-table>
    </div>
    <el-pagination class="footer" v-if="total" :disabled="!props.showPagination && total ? false :true" v-model:current-page="query.page" :page-size="query.size"
      :page-sizes="[50, 100, 200, 400]" layout="total,sizes, prev, pager, next" background :total="total"
      @current-change="handleCurrentChange" @size-change="handleSizeChange"/>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted, nextTick, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { throttle } from '@/utils/index'

import { useStore } from '@/stores/index'
const indexStore = useStore()

const props = defineProps({
  list: {
    //  table表格数据
    type: Array,
    default: () => {
      return []
    }
  },
  query: {
    // from 查询数据
    type: Object,
    default: () => {
      return {
        page: 1,
        size: 20
      }
    }
  },
  total: {
    //  分页总数
    type: Number,
    default: () => {
      return 0
    }
  },
  showForm: {
    //  控制【查询菜单】是否显示
    type: Boolean,
    default: () => {
      return true
    }
  },
  showFromQuery: {
    //  控制【查询按钮】是否显示
    type: Boolean,
    default: () => {
      return true
    }
  },
  showFromButton: {
    //  控制【自定义按钮】是否显示
    type: Boolean,
    default: () => {
      return true
    }
  },
  showExport: {
    //  控制【导出】是否显示
    type: Boolean,
    default: () => {
      return false
    }
  },
  showSet: {
    //  控制【表格表头】是否显示
    type: Boolean,
    default: () => {
      return true
    }
  },
  showPanel: {
    //  仪表盘
    type: Boolean,
    default: () => {
      return false
    }
  },
  showPagination: {
    // 分页
    type: Boolean,
    default: () => {
      return false
    }
  },
})
const data = reactive({
  tableContentHeight: '' as string | number, //表格高度
  query: {
    agent_id: -1,
    operated_id: -1,
  },
})

onMounted(async () => {
  initTableMaxHeight()
})

const tableOtherRef = ref<any>()
const tableContainerRef = ref<any>()
const tableHeaderRef = ref<any>()
const tableButtonRef = ref<any>()
const initTableMaxHeight = () => {
  nextTick(()=>{
    const tableHeight = tableContainerRef.value.offsetHeight
    const tableOtherHeight = tableOtherRef.value.offsetHeight
    const tableHeaderHeight = tableHeaderRef.value.offsetHeight
    const tableButtonHeight = tableButtonRef.value.offsetHeight
    const tableFooterRefHeight = 52
    const height = tableHeight - tableOtherHeight - tableHeaderHeight - tableButtonHeight - tableFooterRefHeight
    data.tableContentHeight = (height > 0 ? height : tableHeight) + 'px'
    // console.log('页面高度：',tableHeight,'表头other：',tableHeaderHeight,'表头Header：',tableHeaderHeight,'表头Button：',tableButtonHeight,'表尾巴高度：',tableFooterRefHeight,'表格高度：',height)
  })
}

let key = ref();
const emit = defineEmits(['changePage', 'changeSize', 'search', 'changeTh', 'reset', 'sort', 'download','select'])

// 改变分页页数
const handleCurrentChange = throttle((val: number) => {
  emit('changePage', val)
  key.value = Math.random()
}, 500)
// 改变分页条数
const handleSizeChange = throttle((val: number) => {
  emit('changeSize', val)
  handleCurrentChange(1)
  key.value = Math.random()
}, 500)

// 查询
const getSearchList = throttle(() => {
  emit('search')
}, 500)
// 重置
const getResetList = throttle(() => {
  emit('reset')
}, 500)

// 刷新
const refreshPage = throttle(() => {
  emit('search')
  ElMessage.success('刷新成功')
}, 500)

// 行内数据高亮
const tableRowClassName = (data:any) => {
  return data.row.className?data.row.className:''
}

// 下载
const exportDownload = throttle(() => {
  if(!props.list || !props.list.length) return false
  emit('download')
}, 1000)

const selectTable = (val: any) => {
  emit('select', val)
}

// 排序
interface Column {
  order: string,
  prop: string
}
const sortTable = (column: Column) => {
  if(column.order == 'descending'){
    column.order = 'desc'
  }else{
    column.order= 'asc'
  }
  emit('sort', column)
}
const changeTh = () => {
  emit('changeTh')
}
defineExpose({
  initTableMaxHeight
})
</script>
<style scoped lang="scss">
.flex-box {
  display: flex;
  flex-direction: column;
  height: 100%;
}
.search-box {
  background: var(--background);
  padding: 10px 10px 0 10px;
  border-bottom: 2px solid #ededed;
  border-top-left-radius: 8px;
  border-top-right-radius: 8px;
}
.table-box{
  max-height: 100%;
  min-height: 550px;
  // overflow: hidden;
  padding:10px 5px 0 5px;
  border-bottom-left-radius: 8px;
  border-bottom-right-radius: 8px;
  color: var(--text);
  background: var(--background);
  .icon-box{
    display: flex;
    flex-direction: row-reverse;
    align-items: flex-start;
    .icon-item{
      margin-right: 10px;
      cursor: pointer;
    }
    .icon-item :hover{
      scale: (1.1);
      all: inherit 0.1s;
    }
  }
}
.search-button {
  margin: -5px 0 0px 0;
}
// 设置表头
.operation {
  width: 50px;
  cursor: pointer;
  position: absolute;
  top: 10px;
  right: 15px;
  text-align: center;
}
.footer{
  padding-top: 10px;
  z-index: 100;
}
</style>